﻿using System;
using System.Windows.Forms;

namespace Texpro
{
    public partial class FMMN101 : Form
    {
        public FMMN101()
        {
            InitializeComponent();
        }

        private void FMMN101_Load(object sender, EventArgs e)
        {
            UseWaitCursor = true;
            try
            {
                if (!PCPUB.m_read_config_file())
                {
                    PCMSG.ShowError("Texpro: Error while reading application cofiguration from file. Terminating application abnormally.");
                    Application.Exit();
                }
                else if (!PCPUB.m_read_config_data())
                {
                    PCMSG.ShowError("Texpro: Error while reading application cofiguration from server. Terminating application abnormally.");
                    Application.Exit();
                }
                else if (!PCPUB.m_verify_version())
                {
                    PCMSG.ShowError("Texpro: Product or Version mismatched with server. Terminating application abnormally.");
                    Application.Exit();
                }
                else if (!PCPUB.m_verify_version())
                {
                    PCMSG.ShowError("Texpro: Licence key mismatched. Terminating application abnormally.");
                    Application.Exit();
                }
                load_form();
                clear_form();
                txt_user_id.Focus();
            }
            catch (Exception ex)
            {
                PCMSG.ShowError(ex);
                PCMSG.ShowError("Error occured. Terminating application abnormally");
                Application.Exit();
            }
            UseWaitCursor = false;
        }

        private bool load_form()
        {
            bool b_return = false; 
            UseWaitCursor = true;
            try
            {
                this.Text = PCPUB.mc_app_product;
                lbl_form_title.Text = "Login";
                lbl_version.Text = PCPUB.mc_app_version + " (" + PCPUB.mc_app_build + ")";
                lbl_license.Text = "Licensed to:" + "\n" + PCPUB.mc_org_name + "\n" + PCPUB.mc_org_city;
                b_return = true;
            }
            catch (Exception ex)
            {
                PCMSG.ShowError(ex);
            }
            return b_return;
        }

        private bool validate_form()
        {
            bool b_error = false;
            ep_form.Clear();
            if (txt_user_id.Text == String.Empty)
            {
                //PCMSG.Show("Username field should not be empty.","Login",MessageBoxButtons.OK,MessageBoxIcon.Information);
                ep_form.SetError(txt_user_id, "Username field should not be empty.");
                b_error = true;
            }
            if (txt_user_password.Text == String.Empty)
            {
                //PCMSG.Show("Password field should not be empty.","Login",MessageBoxButtons.OK,MessageBoxIcon.Information);
                ep_form.SetError(txt_user_password, "Password field should not be empty.");
                b_error = true;
            }
            if (b_error)
            {
                PCMSG.Show("Error(s) occured. Please check and clear errors.","Login",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            if (ep_form.GetError(txt_user_id) != String.Empty)
            {
                txt_user_id.Focus();
                return false;
            }
            else if (ep_form.GetError(txt_user_password) != String.Empty)
            {
                txt_user_password.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }

        private void clear_form()
        {
            PCPUB.m_user_id = String.Empty;
            PCPUB.m_user_name = String.Empty;
            PCPUB.m_user_role_id = String.Empty;
            PCPUB.m_user_logged_in = false;
            txt_user_id.Text = String.Empty; //"U1";
            txt_user_password.Text = String.Empty;  // "TEST";
            ep_form.Clear();
        }

        private void txt_username_Validated(object sender, EventArgs e)
        {
            if (txt_user_id.Text == String.Empty)
            {
                ep_form.SetError(txt_user_id, "Username field should not be empty.");
            }
        }

        private void txt_user_password_Validated(object sender, EventArgs e)
        {
            if (txt_user_password.Text == String.Empty)
            {
                ep_form.SetError(txt_user_password, "Password field should not be empty.");
            }
        }

        private void but_ok_Click(object sender, EventArgs e)
        {
            UseWaitCursor = true;
            if (validate_form())
            {
                string user_id = txt_user_id.Text.Trim().ToUpper();
                string user_password = txt_user_password.Text.Trim().ToUpper();
                if (PCDB.tis_user_validate(user_id, user_password))
                {
                    PCPUB.m_user_logged_in = true;
                    this.Close();
                }
                else
                {
                    clear_form();
                    UseWaitCursor = false;
                    PCMSG.Show("Invalid Username or Password.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_user_id.Focus();
                }
            }
            UseWaitCursor = false;
        }

        private void but_exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void text_id_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back))
            {
                e.Handled = true;
            }
        }

        private void text_gen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space || e.KeyChar == '.'))
            {
                e.Handled = true;
            }
        }

        private void text_int_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back))
            {
                e.Handled = true;
            }
        }

        private void text_dec_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.'))
            {
                e.Handled = true;
            }
        }

        private void FMMN101_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (PCPUB.m_user_logged_in)
            {
                // do nothing
            }
            else
            {
                Application.Exit();
            }
        }

    }

}